/*
BPK000.cpp
Jeff Eaton - 2014
BPK-000 Serial Interface for Text LCDs
http://www.seetron.com/bpk000.html

This software uses the SendOnlySoftwareSerial library adapted from SoftwareSerial by Nick Gammon 28th June 2012
http://gammon.com.au/Arduino/SendOnlySoftwareSerial.zip

- MIT LICENSE -
Copyright (c) 2014 Jeff Eaton

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include <Arduino.h>
#include "BPK000.h"

// 254 (0xFE) is the instruction byte and must be sent before any instruction
// otherwise the BPK will interpret the received data as text and put it on the screen

BPK000::BPK000(int pin, int baudRate) : _serialInterface(pin, true) { // true argument for inverted TTL
	pinMode(pin, OUTPUT);
	digitalWrite(pin, LOW);
	_baudRate = baudRate;
	_serialPin = pin;
	_serialInterface.begin(baudRate);
}

BPK000::~BPK000() {
	
}

// all codes found on page 4 of http://www.seetron.com/docs/bpk000mnl.pdf

void BPK000::clear() {
	_serialInterface.write(254);
	_serialInterface.write(1);
	if(_baudRate > 2400) {	// According to the datasheet, both the clear and home commands take more than 1ms for the LCD to execute.
		delay(2);			// It recommends at least 1ms of delay after each of these commands, so 2ms delay should be okay.
	}						// No delay is required at 2400bps.
}

void BPK000::blank() { // blanks the display without clearing its data
	_serialInterface.write(254);
	_serialInterface.write(8);
}

void BPK000::restore() { // restores screen after a blank, with cursor hidden
	_serialInterface.write(254);
	_serialInterface.write(12); // actually the same code as hide cursor code
}

void BPK000::home() {
	_serialInterface.write(254);
	_serialInterface.write(2);
	if(_baudRate > 2400) {
		delay(2);
	}
}

void BPK000::hideCursor() {
	_serialInterface.write(254);
	_serialInterface.write(12);
}

void BPK000::showCursor(int type) { // 0 for underline cursor, anything else for block cursor
	_serialInterface.write(254);
	if(type == 0)
		_serialInterface.write(14);
	else
		_serialInterface.write(13);
}

void BPK000::moveCursorLeft() {
	_serialInterface.write(254);
	_serialInterface.write(16);
}

void BPK000::moveCursorRight() {
	_serialInterface.write(254);
	_serialInterface.write(20);
}

void BPK000::scrollLeft() {
	_serialInterface.write(254);
	_serialInterface.write(24);
}

void BPK000::scrollRight() {
	_serialInterface.write(254);
	_serialInterface.write(28);
}

void BPK000::setPosition(int row, int column) {
	_serialInterface.write(254);
	switch(row) {
		case 0: _serialInterface.write(128 + column); break;
		case 1: _serialInterface.write(192 + column); break;
		case 2: _serialInterface.write(148 + column); break;
		case 3: _serialInterface.write(212 + column); break;
		default: _serialInterface.write(128+ column); break;
	}
}

void BPK000::print(char s[]) {
	_serialInterface.print(s);
}